from django.contrib.auth.models import AbstractUser
from django.db import models
from ERP.utils.base_model import BaseModel


# Create your models here. children

class MenuModel(BaseModel):
    """功能菜单的模型类"""
    number = models.IntegerField('排序数据', null=True, blank=True)
    url = models.CharField('URL地址', max_length=250, null=True, blank=True)
    name = models.CharField('菜单名字', max_length=50)
    # 不会真正的删除数据库中的数据，默认是0,如果是1则表示删除
    delete_flag = models.CharField('删除标记', max_length=3, default='0')
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children', on_delete=models.CASCADE)

    class Meta:
        db_table = 't_menu'
        verbose_name = '功能菜单表'
        verbose_name_plural = verbose_name
        ordering = ['number']

    def __str__(self):
        return f'{self.number},{self.url},{self.name},{self.delete_flag}'


# 用户的模型类
class UserModel(AbstractUser, BaseModel):
    phone = models.CharField("手机号", max_length=11, unique=True, null=True, blank=True)
    real_name = models.CharField("真实名字", max_length=50, null=True, blank=True)

    # 用户所有的角色
    roles = models.ManyToManyField('RolesModel', db_table='t_users_roles', blank=True,
                                   verbose_name="用户跟角色之间的多对多关联关系")

    class Meta:
        db_table = 't_user'
        verbose_name = "系统用户表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username + ":" + self.real_name


class PermissionsModel(BaseModel):
    """权限的模型类，包含两个要素，资源（父菜单，接口），操作"""
    method_choices = (
        ('POST', '增'),
        ('DELETE', '删'),
        ('PUT', '改'),
        ('PATCH', '局部修改'),
        ('GET', '查询'),
    )
    """
    只要是choices参数字段，如果你想要获取对应的key和value,默认的是获取KEY
    如果需要获取value，可以采取get_字段名_display()
    print(permission.method) 获取key
    print(permission.method.get_method_display()) 获取value
    """
    name = models.CharField('权限名称', max_length=50)
    is_menu = models.BooleanField('名称', default=True)  # True 就是菜单，False 就是接口
    # 操作
    method = models.CharField('操作', max_length=8, blank=True, null=True, choices=method_choices)
    path = models.CharField('访问URL地址', max_length=256, null=True, blank=True)
    decs = models.CharField('权限的描述', max_length=512, null=True, blank=True)
    menu = models.ForeignKey('MenuModel', null=True, blank=True, related_name='permissions_list',
                             on_delete=models.CASCADE)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 't_permissions'
        verbose_name = '权限表'
        verbose_name_plural = verbose_name
        ordering = ['id']


class RolesModel(BaseModel):
    """
    角色模型
    """
    name = models.CharField('角色名称', unique=True, max_length=50)
    # 设置多对多的关联关系 用户跟权限
    permissions = models.ManyToManyField('PermissionsModel', db_table='t_roles_permissions',
                                         blank=True, verbose_name="角色的权限")

    def __str__(self):
        return self.name

    class Meta:
        db_table = 't_roles'
        verbose_name = "角色表"
        verbose_name_plural = verbose_name
        ordering = ['id']
